package productmanagementclient.db.adapter;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import productmanagementclient.db.utils.SessionFactoryUtil;
import productmanagementclient.publish.User;

public class UserAdapter {
	
	private static UserAdapter instance = new UserAdapter();
	
	private UserAdapter() {
		System.setProperty("org.xml.sax.driver","org.apache.crimson.parser.XMLReaderImpl");
	}
	
	public static UserAdapter getInstance() {
		return instance;
	}
	public List<User> getAllUser() {
		Session session = SessionFactoryUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<User> list = null;
		try {
			transaction = session.beginTransaction();
			Query query = session.createQuery("from User");
			list = query.list();
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

		return list;
	}
	
	public User getUserById(Integer userID) {
		Session session = SessionFactoryUtil.getSessionFactory().openSession();
		Transaction transaction = null;		
		User user = null;
		try {
			transaction = session.beginTransaction();
			user = (User) session.get(User.class, userID);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

		return user;
	}
	
	public User getUserByName(String userName) {
		Session session = SessionFactoryUtil.getSessionFactory().openSession();
		Transaction transaction = null;		
		User user = null;
		try {
			transaction = session.beginTransaction();
			//user = (User) session.get(User.class, userID);			
			Query query = session.createQuery("from User as u where u.userName = :userName");			
		    query.setString("userName", userName);
		    user = (User) query.uniqueResult();			
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				transaction.rollback();
			}
			throw new IllegalArgumentException("Cannot connect to data base");
		} finally {
			session.close();
		}

		return user;
	}

	public Long saveUser(User user) {
		Session session = SessionFactoryUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Long userId = null;
		try {
			transaction = session.beginTransaction();
			userId = (Long) session.save(user);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return userId;
	}
	
	public void updateUser(User newUser) {
		Session session = SessionFactoryUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.update(newUser);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	public void deleteUser(Long userId) {
		Session session = SessionFactoryUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			User user = (User) session.get(User.class, userId);
			if (user != null)
				session.delete(user);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
}
